package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeEmployment;
import org.edu.modules.sxjy.service.IVeEmploymentService;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VeEmploymentAddVo;
import org.edu.modules.sxjy.vo.VeEmploymentUpdateVo;
import org.edu.modules.sxjy.vo.VeEmploymentVo;
import org.edu.modules.sxjy.vo.VeEmploymentVoXls;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"就业信息管理"})
@RestController
@RequestMapping({"/sxjy/veEmployment"})
@Component
@ApiSort(60)
public class VeEmploymentController
        extends BaseController<VeEmployment, IVeEmploymentService>
{
    private static final Logger log = LoggerFactory.getLogger(VeEmploymentController.class);
    @Autowired
    private IVeEmploymentService veEmploymentService;

    @AutoLog("就业信息管理-导入数据")
    @ApiOperation(value="导入数据", notes="就业信息管理-导入数据")
    @RequestMapping(value={"/importExcel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)
    {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        Iterator localIterator = fileMap.entrySet().iterator();
        if (localIterator.hasNext())
        {
            Map.Entry<String, MultipartFile> entity = (Map.Entry)localIterator.next();
            MultipartFile file = (MultipartFile)entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(true);
            int size = 0;
            try
            {
                List<VeEmploymentVoXls> listSysCategorys = ExcelImportUtil.importExcel(file.getInputStream(), VeEmploymentVoXls.class, params);
                //lbh 2024/07/31 localObject1 >> Iterator localObject1
                for (Iterator localObject1 = listSysCategorys.iterator(); ((Iterator)localObject1).hasNext();)
                {
                    VeEmploymentVoXls veEmploymentVoXls = (VeEmploymentVoXls)((Iterator)localObject1).next();
                    boolean b = this.veEmploymentService.setExcel(veEmploymentVoXls);
                    if (b) {
                        size++;
                    }
                }
                return Result.ok("文件导入成功！数据行数：" + size);
            }
            catch (Exception e)
            {
                Object localObject1;
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败：" + e.getMessage());
            }
            finally
            {
                try
                {
                    file.getInputStream().close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return Result.error("文件导入失败！");
    }

    @AutoLog("就业信息管理-查询列表")
    @ApiOperation(value="查询列表", notes="就业信息管理-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VeEmploymentVo veEmploymentVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeEmploymentVo> veEmploymentVoList = this.veEmploymentService.selectAll(veEmploymentVo, startLine, pageSize, veCommon);
        int total = this.veEmploymentService.getSumPage(veEmploymentVo, veCommon);
        if ((Objects.isNull(veEmploymentVoList)) && (veEmploymentVoList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.error("未找到对应数据"));
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veEmploymentVoList));
    }

    @AutoLog("就业信息管理-导出数据")
    @ApiOperation(value="导出数据", notes="就业信息管理-导出数据")
    @PostMapping({"/exportXls"})
    public ModelAndView exportXls()
    {
        List<VeEmploymentVoXls> veEmploymentVoXlsList = this.veEmploymentService.exportXls();

        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());

        modelAndView.addObject("fileName", "就业信息");
        modelAndView.addObject("entity", VeEmploymentVoXls.class);
        LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        modelAndView.addObject("params", new ExportParams("就业信息列表", "导出人:" + user.getRealname(), "导出信息"));
        modelAndView.addObject("data", veEmploymentVoXlsList);
        return modelAndView;
    }

    @AutoLog("就业信息管理-就业信息统计")
    @ApiOperation(value="就业信息统计", notes="就业信息管理-就业信息统计")
    @GetMapping({"/countEmploy"})
    public Result<?> countEmploy(@RequestParam(name="njId", required=true) int njId, @RequestParam(name="specId", required=true) int specId)
    {
        return this.veEmploymentService.getEntityCount(njId, specId);
    }

    @AutoLog("就业信息管理-批量删除")
    @ApiOperation(value="批量删除", notes="就业信息管理-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        return this.veEmploymentService.deleteBatch(ids);
    }

    @AutoLog("就业信息管理-学籍")
    @ApiOperation(value="学籍", notes="就业信息管理-学籍")
    @GetMapping({"/selectSchool"})
    public Result<?> selectSchool(int id)
    {
        return this.veEmploymentService.getSchool(id);
    }

    @AutoLog("就业信息管理-查看详情")
    @ApiOperation(value="查看详情", notes="就业信息管理-查看详情")
    @GetMapping({"/selectOneInfo"})
    public Result<?> selectOneInfo(int id)
    {
        return this.veEmploymentService.getOneInfo(id);
    }

    @AutoLog("就业信息管理-新增信息")
    @ApiOperation(value="新增信息", notes="就业信息管理-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeEmploymentAddVo veEmploymentAddVo)
    {
        if (veEmploymentAddVo != null)
        {
            this.veEmploymentService.saves(veEmploymentAddVo);
            return Result.OK("添加成功！");
        }
        return Result.error("添加失败");
    }

    @AutoLog("就业信息管理-编辑回显")
    @ApiOperation(value="编辑回显", notes="就业信息管理-编辑回显")
    @GetMapping({"/udpateEcho"})
    public Result<?> updateEcho(int id)
    {
        if (id != 0)
        {
            VeEmploymentUpdateVo veEmploymentUpdateVo = this.veEmploymentService.editEcho(id);
            return Result.OK(veEmploymentUpdateVo);
        }
        return Result.error("编辑失败");
    }

    @AutoLog("就业信息管理-编辑")
    @ApiOperation(value="编辑", notes="就业信息管理-编辑")
    @PutMapping({"/edit"})
    public Result<?> edit(@RequestBody VeEmploymentUpdateVo veEmploymentUpdateVo)
    {
        if (veEmploymentUpdateVo != null)
        {
            this.veEmploymentService.edit(veEmploymentUpdateVo);
            return Result.OK("编辑成功！");
        }
        return Result.error("失败!");
    }

    @AutoLog("就业信息管理-搜索学生信息")
    @ApiOperation(value="搜索学生信息", notes="就业信息管理-搜索学生信息")
    @GetMapping({"/stuInfoSearch"})
    public PageResponseResult stuInfoSearch(@RequestParam(name="interfaceUserId", required=true) String interfaceUserId, @RequestParam(name="xm", required=false) String xm, @RequestParam(name="xh", required=false) String xh, @RequestParam(name="sfzh", required=false) String sfzh, @RequestParam(name="falId", required=false) Integer falId, @RequestParam(name="specId", required=false) Integer specId, @RequestParam(name="gradeId", required=false) Integer gradeId, @RequestParam(name="bjId", required=false) Integer bjId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        return this.veEmploymentService.stuInfoSearch(interfaceUserId, xm, xh, sfzh, falId, specId, gradeId, bjId, pageNo, pageSize);
    }
}
